//
//  TagsEditViewController.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/2/1.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

class TagsEditViewController: BasePresentedViewController {

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: 50, height: 50)
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        layout.sectionInset = 5.insets
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.registerCellClass(with: AttributesCell.self)
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.backgroundColor = .white
        collectionView.dataSource = self
        return collectionView
    }()

    lazy var colorLabel = self.label()
    lazy var contentLabel = self.label()

    let contentTextFiled: UITextField = {
        let textFiled = UITextField()
        textFiled.textAlignment = .left
        textFiled.font = UIFont.systemMediumFont(withSize: 12)
        textFiled.textColor = UIColor.themeDarkGrayColor
        textFiled.addLine(on: .bottom, color: UIColor(hexString: "#cccccc")!, lineWidth: 1, inset: UIEdgeInsets(top: 0, left: -70, bottom: 0, right: 0))
        return textFiled
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }
}

private extension TagsEditViewController {

    func setupViews() {

        colorLabel.text = "颜色选择:"
        contentLabel.text = "内容输入:"
        bgImage = #imageLiteral(resourceName: "custom_embroidery_bg")
        confirmButtonStyle = .lowerRight
        confirmButton.setTitle("保存", for: .normal)

        contentView.addSubview(colorLabel)
        contentView.addSubview(collectionView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(contentTextFiled)

        colorLabel.mas_makeConstraints { (make) in
            make?.left.top().equalTo()(contentView)
            make?.height.equalTo()(17)
            make?.width.equalTo()(65)
        }
        
        collectionView.mas_makeConstraints { (make) in
            make?.left.right().equalTo()(contentView)
            make?.top.equalTo()(colorLabel.mas_bottom)?.offset()(4)
            make?.height.equalTo()(115)
        }
        contentLabel.mas_makeConstraints { (make) in
            make?.left.equalTo()(contentView)
            make?.top.equalTo()(collectionView.mas_bottom)?.offset()(25)
            make?.height.equalTo()(17)
            make?.width.equalTo()(65)
        }
        contentTextFiled.mas_makeConstraints { (make) in
            make?.left.equalTo()(contentLabel.mas_right)?.offset()(5)
            make?.right.equalTo()(collectionView)
            make?.height.equalTo()(33)
            make?.centerY.equalTo()(contentLabel)
        }
    }
}

extension TagsEditViewController {
    func label() -> UILabel {
        let label = UILabel()
        label.font = UIFont.systemMediumFont(withSize: 12)
        label.textColor = UIColor.themeDarkGrayColor
        label.textAlignment = .left
        return label
    }
}

extension TagsEditViewController: UICollectionViewDelegate, UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: AttributesCell = collectionView.dequeueReusableCell(for: indexPath)
        cell.imageView.image = UIImage(by: UIColor.random(), size: CGSize(width: 50, height: 50), cornerRadius: 8, borderWidth: 0, borderColor: nil, stretch: false)
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    }
}
